/* $Id: LocaleEntry.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;
import alt.djudge.frontend.shared.dto.admin.LocaleEntryDTO;


@PersistenceCapable(detachable="true")
public class LocaleEntry extends AbstractEntry
{
	private static final long serialVersionUID = 1L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private Long parentId = null;
	
	@Persistent
	private String javaLocale = null;
	
	@Persistent
	private String sid = null;
	
	@Persistent
	private String name = null;
	
	@Persistent
	private Boolean isDefault = false;
	
	public void setParentId(Long parentId)
	{
		this.parentId = parentId;
	}

	public Long getParentId()
	{
		return parentId;
	}

	public void setJavaLocale(String javaLocale)
	{
		this.javaLocale = javaLocale;
	}

	public String getJavaLocale()
	{
		return javaLocale;
	}

	public void setSid(String sid)
	{
		this.sid = sid;
	}

	public String getSid()
	{
		return sid;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setIsDefault(Boolean isDefault)
	{
		this.isDefault = isDefault;
	}

	public Boolean getIsDefault()
	{
		return isDefault;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if (o instanceof LocaleEntry)
		{
			return ((LocaleEntry) o).getJavaLocale().equalsIgnoreCase(getJavaLocale());
		}
		return super.equals(o);
	}
	
	@Override
	public void fromDTO(AdminDTOEntry dtoEntry)
	{
		setParentId(Long.parseLong(dtoEntry.getFieldValue(1)));
		setJavaLocale(dtoEntry.getFieldValue(2));
		setSid(dtoEntry.getFieldValue(3));
		setName(dtoEntry.getFieldValue(4));
		setIsDefault(Boolean.parseBoolean(dtoEntry.getFieldValue(5)));
	}

	@Override
	public AdminDTOEntry ownAdminDTO()
	{
		String[] data = new String[] {
			getId().toString(),
			getParentId().toString(),
			getJavaLocale(),
			getSid(),
			getName(),
			getIsDefault().toString(),
		};
		return new LocaleEntryDTO(data);
	}
}
